package Handler;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import DAO.*;
import Modelo.*;

@WebServlet("/ViajeHandler")
public class ViajeHandler extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	private static String ADD = "/ViajeAdd.jsp";
    private static String Edit = "/Viajeedit.jsp";
    private static String ViajeRecord="/ListViaje.jsp";
    private ViajeDao dao;
    
    public ViajeHandler() {
        super();
        dao = new ViajeDao();
    }

	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String redirect="";       
        String action = request.getParameter("action");
        
		if (action.equalsIgnoreCase("editform")){         
		        redirect = Edit;            
		}
		else if (action.equalsIgnoreCase("addnew")){         
		        redirect = ADD;            
		}else if (action.equalsIgnoreCase("listViaje")) {
			redirect = ViajeRecord;
			request.setAttribute("Viaje", dao.getAllViajes());
		} 
		
		 RequestDispatcher rd = request.getRequestDispatcher(redirect);
		 rd.forward(request, response);
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String redirect="";
        String combId = request.getParameter("ViajeId");        
        String action = request.getParameter("action");
        
		if (action.equalsIgnoreCase("insert")) {
			Viaje objViaje = new Viaje();
			objViaje.setLugarOrigenId(Integer.parseInt(request.getParameter("lugarOrigenId")));
			objViaje.setLugarDestinoId(Integer.parseInt(request.getParameter("lugarDestinoId")));
			objViaje.setRuta(request.getParameter("Ruta"));
			objViaje.setDistancia(Float.parseFloat(request.getParameter("Distancia")));
			dao.addViaje(objViaje);
			redirect = ViajeRecord;
			request.setAttribute("Viaje", dao.getAllViajes());
			System.out.println("Record Added Successfully");
		}

		else if (action.equalsIgnoreCase("delete")) {
			String Viaje_Id = request.getParameter("ViajeId");
			int vid = Integer.parseInt(Viaje_Id);
			dao.removeViaje(vid);
			redirect = ViajeRecord;
			request.setAttribute("Viaje", dao.getAllViajes());
			System.out.println("Record Deleted Successfully");

		} else if (action.equalsIgnoreCase("edit")) {
			String Viaje_Id = request.getParameter("ViajeId");
			int vid = Integer.parseInt(Viaje_Id);
			Viaje objViaje = new Viaje();
			objViaje.setViajeId(vid);
			objViaje.setLugarOrigenId(Integer.parseInt(request.getParameter("lugarOrigenId")));
			objViaje.setLugarDestinoId(Integer.parseInt(request.getParameter("lugarDestinoId")));
			objViaje.setRuta(request.getParameter("Ruta"));
			objViaje.setDistancia(Float.parseFloat(request.getParameter("Distancia")));
			request.setAttribute("Viaje", objViaje);
			redirect = ViajeRecord;
			System.out.println("Record updated Successfully");

		} else if (action.equalsIgnoreCase("listViaje")) {
			redirect = ViajeRecord;
			request.setAttribute("Viaje", dao.getAllViajes());
		} else {
			redirect = ADD;
		}

    RequestDispatcher rd = request.getRequestDispatcher(redirect);
    rd.forward(request, response);
	}

}
